<?php

namespace app\admin\controller;

use app\common\model\ExamList;
use app\common\model\ExamStudent;
use think\Controller;
use think\Request;
use think\Session;

class Exam extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }



    public function lst()
    {
        $artres = ExamList::paginate('20');
        $this->assign('artres', $artres);
        $this->assign('num', count($artres));
        $this->assign('method', __METHOD__);
        return view('examManage/lst');
    }

    public function mark($id)
    {
        if (Request::instance()->isGet()) {
            $artres = ExamList::get($id);
            $questions = ExamStudent::get($id);
            $qall = json_decode($questions->qid);
            $q5 = $qall->q5;
            $ques = array();
            foreach ((array)$q5 as $k => $q) {
                $ques[] = \app\common\model\Qbank::get($q)->content;
//

            }
            $this->assign('art', $artres);
            $this->assign('ques', $ques);
            return view('examManage/edit');
        } elseif (Request::instance()->isPost()) {
            $data = Request::instance()->post();
            $sco = '';
            foreach ($data as $score) {
                $sco .= $score . ',';
            }
//            dump(Session::get('id'));
            $sco = substr($sco, 0, strlen($sco) - 1);
            $exam = ExamList::get($id);
            $exam->admin_id=Session::get('id');
            $exam->subject_score = $sco;
            $exam->save();

            $this->success('评分完成');
        }
    }

    public function showExamList($id)
    {
        $exam_student = ExamList::get($id);
        $exam_paper = ExamStudent::get($id);
        $ans = json_decode($exam_student->stu_ans);
        $qpaper = json_decode($exam_paper->qid);
        $q5paper = $qpaper->q5;
        foreach ((array)$q5paper as $qn => $q5) {
            $questions[$qn] = \app\common\model\Qbank::get($q5)->content;
            $ans[$qn] = $ans->q5->$qn;
        }

        $this->assign('questions', $questions);
        $this->assign('answer', $ans);
        return view('');
    }

    public function giveScore($id)
    {
        $data = Request::instance()->post('score');
        $sco = '';
        foreach ($data as $score) {
            $sco .= $score . ',';
        }
        $sco = substr($sco, 0, strlen($sco) - 1);
        $exam = ExamList::get($id);
        $exam->subject_score = $sco;
        $exam->save();
        $this->success('评分完成');
    }
    public function delete(){
        if (ExamList::destroy(input('id'))) {
            $this->success('删除文章成功！');
        } else {
            $this->error('删除文章失败！');
        }

    }
//        ExamStudent::destroy(input('id'));
//        ExamList::destroy(input('id'));


}
